// 模拟图书数据
const books = [
    {
      id: 1,
      title: 'JavaScript高级程序设计',
      author: 'Matt Frisbie',
      price: 99.9,
      rating: 4.8,
      cover: 'https://picsum.photos/id/24/300/450',
      category: '编程',
      description: 'JavaScript权威指南，前端开发者必备。全面讲解JavaScript语言核心特性、高级概念和实践技巧。',
      stock: 100,
      publicationDate: '2021-05-10',
      publisher: '人民邮电出版社',
      pages: 864,
      language: '中文',
      isbn: '9787115564281',
    },
    {
      id: 2,
      title: 'Vue.js实战',
      author: '梁灏',
      price: 79.9,
      rating: 4.6,
      cover: 'https://picsum.photos/id/25/300/450',
      category: '编程',
      description: '全面讲解Vue.js框架的实战指南，涵盖Vue核心概念、组件化开发、状态管理、路由等内容。',
      stock: 50,
      publicationDate: '2020-03-01',
      publisher: '人民邮电出版社',
      pages: 448,
      language: '中文',
      isbn: '9787115517881',
    },
    {
      id: 3,
      title: '三体',
      author: '刘慈欣',
      price: 56.0,
      rating: 4.9,
      cover: 'https://picsum.photos/id/26/300/450',
      category: '科幻',
      description: '中国科幻文学的里程碑之作，讲述了人类与外星文明三体人的首次接触，探讨了宇宙社会学和人类命运。',
      stock: 200,
      publicationDate: '2008-05-01',
      publisher: '重庆出版社',
      pages: 302,
      language: '中文',
      isbn: '9787536692930',
    },
    {
      id: 4,
      title: '活着',
      author: '余华',
      price: 35.0,
      rating: 4.7,
      cover: 'https://picsum.photos/id/27/300/450',
      category: '文学',
      description: '一部关于生命与命运的伟大作品，讲述了在大时代背景下，主人公福贵的人生和家庭不断经受着苦难，到了最后，所有亲人都先后离他而去，仅剩下年老的他和一头老牛相依为命。',
      stock: 150,
      publicationDate: '2012-08-01',
      publisher: '作家出版社',
      pages: 191,
      language: '中文',
      isbn: '9787506365437',
    },
    {
      id: 5,
      title: 'Python数据分析实战',
      author: 'Robert Johnson',
      price: 89.0,
      rating: 4.5,
      cover: 'https://picsum.photos/id/28/300/450',
      category: '编程',
      description: '全面介绍Python数据分析的实用指南，涵盖NumPy、Pandas、Matplotlib等库的使用，以及数据清洗、可视化和机器学习基础。',
      stock: 75,
      publicationDate: '2020-06-01',
      publisher: '机械工业出版社',
      pages: 384,
      language: '中文',
      isbn: '9787111653400',
    },
    {
      id: 6,
      title: '人类简史：从动物到上帝',
      author: '尤瓦尔·赫拉利',
      price: 68.0,
      rating: 4.8,
      cover: 'https://picsum.photos/id/29/300/450',
      category: '历史',
      description: '从全新的角度讲述人类的历史，探讨了认知革命、农业革命和科学革命如何改变了人类和地球。',
      stock: 120,
      publicationDate: '2014-11-01',
      publisher: '中信出版社',
      pages: 448,
      language: '中文',
      isbn: '9787508647357',
    },
    {
      id: 7,
      title: '算法导论（第3版）',
      author: 'Thomas H. Cormen',
      price: 128.0,
      rating: 4.7,
      cover: 'https://picsum.photos/id/30/300/450',
      category: '编程',
      description: '计算机科学领域的经典教材，全面介绍了算法设计与分析的基础知识，包括排序、图算法、动态规划等内容。',
      stock: 40,
      publicationDate: '2012-03-01',
      publisher: '机械工业出版社',
      pages: 972,
      language: '中文',
      isbn: '9787111383279',
    },
    {
      id: 8,
      title: '平凡的世界',
      author: '路遥',
      price: 75.0,
      rating: 4.9,
      cover: 'https://picsum.photos/id/31/300/450',
      category: '文学',
      description: '中国当代文学的经典之作，以中国70年代中期到80年代中期10年间为背景，通过复杂的矛盾纠葛，以孙少安和孙少平两兄弟为中心，展示了普通人在大时代历史进程中所走过的艰难道路。',
      stock: 180,
      publicationDate: '2005-01-01',
      publisher: '北京十月文艺出版社',
      pages: 1125,
      language: '中文',
      isbn: '9787530208044',
    },
  ];
  
  // 获取所有图书
  export const getAllBooks = () => {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve(books);
      }, 500); // 模拟API延迟
    });
  };
  
  // 获取单本图书详情
  export const getBookById = (id) => {
    return new Promise((resolve) => {
      setTimeout(() => {
        const book = books.find(book => book.id === id);
        resolve(book);
      }, 500);
    });
  };
  
  // 按分类获取图书
  export const getBooksByCategory = (category) => {
    return new Promise((resolve) => {
      setTimeout(() => {
        if (category === 'all') {
          resolve(books);
        } else {
          const filteredBooks = books.filter(book => book.category === category);
          resolve(filteredBooks);
        }
      }, 500);
    });
  };
  
  // 获取推荐图书（随机选择）
  export const getRecommendedBooks = () => {
    return new Promise((resolve) => {
      setTimeout(() => {
        const shuffled = [...books].sort(() => 0.5 - Math.random());
        resolve(shuffled.slice(0, 4));
      }, 500);
    });
  };
  
  // 搜索图书
  export const searchBooks = (query) => {
    return new Promise((resolve) => {
      setTimeout(() => {
        if (!query) {
          resolve(books);
        }
        const filteredBooks = books.filter(book => 
          book.title.toLowerCase().includes(query.toLowerCase()) ||
          book.author.toLowerCase().includes(query.toLowerCase())
        );
        resolve(filteredBooks);
      }, 500);
    });
  };
  
  // 获取热门分类
  export const getPopularCategories = () => {
    return [
      { id: 'all', name: '全部' },
      { id: '编程', name: '编程' },
      { id: '文学', name: '文学' },
      { id: '科幻', name: '科幻' },
      { id: '历史', name: '历史' },
    ];
  };

  // 获取相关图书（同分类的其他图书）
export const getRelatedBooks = (bookId) => {
    return new Promise((resolve) => {
      setTimeout(() => {
        // 获取当前图书
        const currentBook = books.find(book => book.id === bookId);
        if (!currentBook) {
          resolve([]);
          return;
        }
        
        // 筛选同分类的其他图书（排除当前图书）
        const relatedBooks = books
          .filter(book => 
            book.category === currentBook.category && 
            book.id !== bookId
          )
          .slice(0, 4); // 最多返回4本
        
        resolve(relatedBooks);
      }, 500);
    });
  };